package com.example.rfidserver.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.rfidserver.pojo.Consume;

import java.util.ArrayList;
import java.util.List;

public class ConsumeDao {

    public List<Consume> getConsumeInfo(){
            SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.rfidserver/files/a.db",null,SQLiteDatabase.OPEN_READONLY);
            Cursor cursor = db.query("consume",null, null, null, null, null, null);
            List consumes = new ArrayList();
            while(cursor.moveToNext()){
                int consume_id = cursor.getInt(0);
                String consume_item_name = cursor.getString(1);
                double consume_integral = cursor.getDouble(2);
                Consume consume = new Consume();
                consume.setConsume_id(consume_id);
                consume.setConsume_Item_name(consume_item_name);
                consume.setConsume_Item_integral(consume_integral);
                consumes.add(consume);
        }
            db.close();
        return  consumes;
    }



    public int addOneService(String consume_Item_name, String consume_Item_integral){
        //权限设置为可都可写
        SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.rfidserver/files/a.db",null,SQLiteDatabase.OPEN_READWRITE);
        ContentValues cv = new ContentValues();
        cv.put("consume_Item_name",consume_Item_name);
        cv.put("consume_Item_integral",consume_Item_integral);
        int res = (int) db.insert("consume", null, cv);
        System.out.println("res:"+res);
        db.close();
        return  res;
    }

    public int  deleteOneServicetByID(int consume_ID){
        //权限设置为可都可写
        SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.rfidserver/files/a.db",null,SQLiteDatabase.OPEN_READWRITE);
        ContentValues cv = new ContentValues();
        String[] args = {String.valueOf(consume_ID)};
        int res = db.delete("consume", "consume_Id=?", args);
        db.close();
        return res;
    }


    //传递过来一个consume对象，我要更新它
    public int updateOneServiceByID(Consume consume){
        int consume_id = consume.getConsume_id();
        //权限设置为可都可写
        SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.rfidserver/files/a.db",null,SQLiteDatabase.OPEN_READWRITE);
        ContentValues cv = new ContentValues();
        cv.put("consume_Item_name",consume.getConsume_Item_name());
        cv.put("consume_Item_integral",consume.getConsume_Item_integral());
        int res = db.update("consume",cv," consume_Id="+consume_id,null);
        db.close();
        return res;
    }

}
